package com.lhf.warnsend.warnsendjson.message;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p></p>
 *
 * @author liuhf
 * @createTime 2021/9/2 15:13
 * @since 1.0
 */
public class RequestContent {

    private RequestHeader head;

    private RequestBody body;

    public RequestContent(RequestHeader head, RequestBody body) {
        this.head = head;
        this.body = body;
    }

    public RequestHeader header() {
        return new RequestHeader();
    }

    public RequestBody body() {
        return new RequestBody();
    }

    public RequestHeader getHead() {
        return head;
    }

    public void setHead(RequestHeader head) {
        this.head = head;
    }

    public RequestBody getBody() {
        return body;
    }

    public void setBody(RequestBody body) {
        this.body = body;
    }

    @Override
    public String toString() {
        JSONObject obj = JSONUtil.createObj();
        JSONObject head = JSONUtil.createObj();
        head.putOpt("channel", this.head.channel);
        head.putOpt("trans_code", this.head.transCode);
        head.putOpt("req_no", this.head.reqNo);
        if (this.head.channelNo != null) {
            head.putOpt("channelNo", this.head.channelNo);
        }
        if (this.head.IdNo != null) {
            head.putOpt("IdNo", this.head.IdNo);
        }
        if (this.head.loanType != null) {
            head.putOpt("loanType", this.head.loanType);
        }

        obj.putOpt("head", head);
        JSONObject body = JSONUtil.createObj();
        obj.putOpt("body", body);

        JSONArray array = JSONUtil.createArray();
        for (Batch batch : this.body.messageInfoList) {
            JSONObject batchObj = JSONUtil.createObj();
            batchObj.putOpt("mobile", batch.mobile);
            batchObj.putOpt("content", batch.content);
            batchObj.putOpt("templateNo", batch.templateNo);
            batchObj.putOpt("smsParam", batch.smsParam);
            array.add(batch);
        }

        body.putOpt("send_type", this.body.sendType);
        if (this.body.scheduleTime != null) {
            body.putOpt("schedule_time", this.body.scheduleTime);
        }
        return obj.toString();
    }

    public static Batch buildBatch() {
        return new Batch();
    }

    public static RequestHeader buildRequestHeader() {
        return new RequestHeader();
    }

    public static RequestBody buildRequestBody() {
        return new RequestBody();
    }

    /**
     * 请求头
     */
    public static class RequestHeader {
        /**
         * 渠道号码
         */
        private String channel;
        /**
         * 交易编码
         */
        private String transCode;
        /**
         * 请求流水号
         */
        private String reqNo;
        /**
         * 核心渠道号
         */
        private String channelNo;
        /**
         * 用户身份证号码
         */
        private String IdNo;
        /**
         * 贷款品种
         */
        private String loanType;

        public RequestHeader channel(String channel) {
            this.channel = channel;
            return this;
        }

        public RequestHeader transCode(String transCode) {
            this.transCode = transCode;
            return this;
        }

        public RequestHeader reqNo(String reqNo) {
            this.reqNo = reqNo;
            return this;
        }

        public RequestHeader channelNo(String channelNo) {
            this.channelNo = channelNo;
            return this;
        }

        public RequestHeader IdNo(String IdNo) {
            this.IdNo = IdNo;
            return this;
        }

        public RequestHeader loanType(String loanType) {
            this.loanType = loanType;
            return this;
        }

    }

    public static class Batch {
        /**
         * 电话号码
         */
        private String mobile;
        /**
         * 内容
         */
        private String content;

        /**
         * smsParam
         */
        private String templateNo;
        /**
         * 短信参数
         */
        private Map<String, String> smsParam;

        public Batch mobile(String mobile) {
            this.mobile = mobile;
            return this;
        }

        public Batch content(String content) {
            this.content = content;
            return this;
        }

        public Batch templateNo(String templateNo) {
            this.templateNo = templateNo;
            return this;
        }

        public Batch smsParam(Map<String, String> smsParam) {
            this.smsParam = smsParam;
            return this;
        }

    }

    public static class RequestBody {


        private List<Batch> messageInfoList;

        /**
         * 短信类型
         */
        private Integer smsType;
        /**
         * 发送方式
         * 1：立即发送 2：预约发送
         */
        private Integer sendType;

        /**
         * 预约发送时间
         * 格式：yyyyMMddHHmmss
         */
        private String scheduleTime;


        public RequestBody messageInfoList(List<Batch> messageInfoList) {
            this.messageInfoList = messageInfoList;
            return this;
        }

        public RequestBody smsType(Integer smsType) {
            this.smsType = smsType;
            return this;
        }


        public RequestBody sendType(Integer sendType) {
            this.sendType = sendType;
            return this;
        }

        public RequestBody scheduleTime(Date scheduleTime) {
            Instant instant = scheduleTime.toInstant();
            LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            this.scheduleTime = dateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
            return this;
        }

    }

}
